# delimit ;
clear ;
set more off ;
version 12.0 ;

cd "replication" ;

* ***************************************************************************** ;
* Carpena, Fenella, and Bilal Zia. "The causal mechanism of financial education: 
* Evidence from mediation analysis." Journal of Economic Behavior & Organization 
* 177 (2020): 143-184.
* 
* This do-file creates Appendix Table A8 of the paper
* ***************************************************************************** ;

program drop _all ;
est drop _all ;
adopath + "`c(pwd)'/code/" ;

* ***************************************************************************** ;
* get endline data
* ***************************************************************************** ;

use "./data/endline.dta", clear ;

* merge with baseline ;
merge 1:1 id using "./data/baseline.dta" ;
tab _merge ;

* correction to the variable label ;
label var savings_bank "has bank savings account" ;

* ***************************************************************************** ;
* sample selection
* ***************************************************************************** ;

* drop households with baseline but no endline ;
drop if _merge == 2 ; 
drop _merge ;

* drop households with no financial knowledge scores ; 
egen temp_knowl = rowmean(numer_fin_return-attit_budget) ;
drop if temp_knowl == . ;
drop temp_knowl ; 

* identify respondents with endline measures of numeracy ;
merge 1:1 id using "./data/financial-knowledge-short-term", keepusing(id) ;
tab _merge ;
keep if _merge == 3 ;
drop _merge ;

* drop households with missing discount rate (control variable) ;
drop if disc_rate == . ;

* ***************************************************************************** ;
* create financial knowledge scores
* ***************************************************************************** ;

egen end_numeracy = rowmean(numer_fin_return-numer_int_rate) ;
egen end_awareness = rowmean(aware_budget-aware_unprod) ;
egen end_attitudes = rowmean(attit_suggest_ins-attit_budget) ;

* ***************************************************************************** ;
* create treatment dummies
* ***************************************************************************** ;

gen flcounsgoal = (fin_ed_treatment == 1 & couns_treatment == 1 & goal_treatment == 1)  ;
gen flcouns = (fin_ed_treatment == 1 & couns_treatment == 1 & goal_treatment == 0) ;
gen flgoal = (fin_ed_treatment == 1 & couns_treatment == 0 & goal_treatment == 1) ;
gen fl = (fin_ed_treatment == 1 & couns_treatment == 0 & goal_treatment == 0) ;
gen control = (fin_ed_treatment == 0 & couns_treatment == 0 & goal_treatment == 0) ;

* ***************************************************************************** ;
* control variables
* ***************************************************************************** ;

local cvars has_hard_time_saving interested_in_financial inconsistent disc_rate riskaverse  ;
local rcvars rhas_hard_time_saving rinterested_in_financial rinconsistent rdisc_rate rriskaverse ;

* ***************************************************************************** ;
* rename variables to make them shorter ;
* ***************************************************************************** ;

ren budget_helpful bdg_help ;
ren budget_tried bdg_tried ;
ren budget_regular bdg_reg ;
ren savings_informal sav_inf ;
ren savings_bank sav_bnk ;
ren loan_purpose_bus_educ loan_bus ; 
ren ins_life ins_life ;

* ***************************************************************************** ;
* make variables to keep the estimates 
* ***************************************************************************** ;

gen y = "" ;
gen m = "" ;
gen t = "" ;
gen acme_se = . ;

order y t m acme_se ;

local row = 1 ; 

* ***************************************************************************** ;
* APPENDIX TABLE A8: PANEL A, FL only 
* ***************************************************************************** ;

est drop _all ;

local t fl ;
local outcomes bdg_tried ; 

* remove strata FEs ;
foreach var of varlist `t' `outcomes' `cvars' end_numeracy end_awareness end_attitudes { ;
	reg `var' i.strata if `t' | control ;
	predict r`var', resid ;
	label var r`var' `var' ;
} ;

* loop over all outcomes and mediators ;
foreach y of local outcomes { ;
	foreach m of varlist end_numeracy end_awareness end_attitudes { ;
		di "*********** treatment: `t', outcome: `y', mediator: `m' ******************* " ;
		medeff_FC	(regress r`m' r`t' `rcvars') 
					(regress r`y' r`t' r`m' `rcvars')
					if `t' | control,
					treat(r`t') mediate(r`m') seed(24533) vce(cluster wave_class) sims(1000) ;
		
		replace y = "`y'" in `row' ; 
		replace m = "`m'" in `row' ;
		replace t = "`t'" in `row' ;
		replace acme_se = r(acme_sd) in `row' ;  
		
		local ++row ; 
				
	} ;
} ;

* ***************************************************************************** ;
* APPENDIX TABLE A8: PANEL B, FL + goal
* ***************************************************************************** ;

est drop _all ;
drop rbdg_tried-rend_attitudes ;

local t flgoal ;
local outcomes bdg_tried sav_bnk ; 

* remove strata FEs ;
foreach var of varlist `t' `outcomes' `cvars' end_numeracy end_awareness end_attitudes { ;
	reg `var' i.strata if `t' | control ;
	predict r`var', resid ;
	label var r`var' `var' ;
} ;

* loop over all outcomes and mediators ;
foreach y of local outcomes { ;
	foreach m of varlist end_numeracy end_awareness end_attitudes { ;
		di "*********** treatment: `t', outcome: `y', mediator: `m' ******************* " ;
		medeff_FC	(regress r`m' r`t' `rcvars') 
					(regress r`y' r`t' r`m' `rcvars')
					if `t' | control,
					treat(r`t') mediate(r`m') seed(24533) vce(cluster wave_class) sims(1000) ;
					
		replace y = "`y'" in `row' ; 
		replace m = "`m'" in `row' ;
		replace t = "`t'" in `row' ;
		replace acme_se = r(acme_sd) in `row' ;  

		local ++row ; 
		
	} ;
} ;

* ***************************************************************************** ;
* APPENDIX TABLE A8: PANEL C, FL + couns
* ***************************************************************************** ;

est drop _all ;
drop rbdg_tried-rend_attitudes ;

local t flcouns ;
local outcomes bdg_tried sav_bnk loan_bus ins_life ; 

* remove strata FEs ;
foreach var of varlist `t' `outcomes' `cvars' end_numeracy end_awareness end_attitudes { ;
	reg `var' i.strata if `t' | control ;
	predict r`var', resid ;
	label var r`var' `var' ;
} ;

* loop over all outcomes and mediators ;
foreach y of local outcomes { ;
	foreach m of varlist end_numeracy end_awareness end_attitudes { ;
		di "*********** treatment: `t', outcome: `y', mediator: `m' ******************* " ;
		medeff_FC	(regress r`m' r`t' `rcvars') 
					(regress r`y' r`t' r`m' `rcvars')
					if `t' | control,
					treat(r`t') mediate(r`m') seed(24533) vce(cluster wave_class) sims(1000) ;
					
		replace y = "`y'" in `row' ; 
		replace m = "`m'" in `row' ;
		replace t = "`t'" in `row' ;
		replace acme_se = r(acme_sd) in `row' ;  

		local ++row ; 
	} ;
} ;

* ***************************************************************************** ;
* APPENDIX TABLE A8: PANEL D, FL + couns + goal
* ***************************************************************************** ;

est drop _all ;
drop rbdg_tried-rend_attitudes ;

local t flcounsgoal ;
local outcomes bdg_tried sav_bnk ins_life ; 

* remove strata FEs ;
foreach var of varlist `t' `outcomes' `cvars' end_numeracy end_awareness end_attitudes { ;
	reg `var' i.strata if `t' | control ;
	predict r`var', resid ;
	label var r`var' `var' ;
} ;

* loop over all outcomes and mediators ;
foreach y of local outcomes { ;
	foreach m of varlist end_numeracy end_awareness end_attitudes { ;
		di "*********** treatment: `t', outcome: `y', mediator: `m' ******************* " ;
		medeff_FC	(regress r`m' r`t' `rcvars') 
					(regress r`y' r`t' r`m' `rcvars')
					if `t' | control,
					treat(r`t') mediate(r`m') seed(24533) vce(cluster wave_class) sims(1000) ;
					
		replace y = "`y'" in `row' ; 
		replace m = "`m'" in `row' ;
		replace t = "`t'" in `row' ;
		replace acme_se = r(acme_sd) in `row' ;  

		local ++row ; 
		
	} ;
} ;

* ***************************************************************************** ;
* calculate MDEs
* ***************************************************************************** ;

keep y t m acme_se ; 
destring acme_se, replace ;
gen mde = round(2.8*acme_se, 0.001) ;

drop acme_se ;
order y t m mde ;
outsheet using "./output/appendix-table-A08.csv", comma names replace ;

exit ;
